package com.gdkf.dormitory.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdkf.dormitory.entity.*;
import com.gdkf.dormitory.service.EmpService;
import com.gdkf.dormitory.tools.MyPageTool;
import com.gdkf.dormitory.vo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcy
 * @since 2022-09-09
 */
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    @PostMapping("/emp/getAllEmp")
    public Msg getAllEmp(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);

        Page<Emp> page = new Page<>(myPage.getCurrentPage(),myPage.getSizePage());
        empService.page(page, null);
        List<Emp> records = page.getRecords();
        return  records.isEmpty()?Msg.fail():Msg.success()
                .add("empList",page.getRecords())
                .add("sizePage",page.getSize())
                .add("currentPage",page.getCurrent())
                .add("pageTotal",page.getTotal());
    }

    @PostMapping("/emp/getOneEmpById")
    public Msg getOneEmpById(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);
        if(myPage.getObject()!=null){
            MyPage myPage2 = MyPageTool.JsonParseToEmp(myPage);
            Page<Emp> page = new Page<>(myPage2.getCurrentPage(),myPage2.getSizePage());
            QueryWrapper<Emp> wrapper = new QueryWrapper<>();
            wrapper.eq("emp_id",((Emp)myPage2.getObject()).getEmpId());
            Page<Emp> page1 = empService.page(page, wrapper);
            List<Emp> records = page1.getRecords();
            return records.isEmpty()?Msg.fail():Msg.success()
                    .add("empList",records)
                    .add("sizePage",page.getSize())
                    .add("currentPage",page.getCurrent())
                    .add("pageTotal",page.getTotal());
        }
        return Msg.fail();
    }

    @PostMapping("/emp/getSomeEmpByName")
    public Msg getSomeEmpByName(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);
        if(myPage.getObject()!=null){
            MyPage myPage2 = MyPageTool.JsonParseToEmp(myPage);
            System.out.println(myPage2);
            Page<Emp> page = new Page<>(myPage2.getCurrentPage(),myPage2.getSizePage());
            QueryWrapper<Emp> wrapper = new QueryWrapper<>();
            String empName = ((Emp) myPage2.getObject()).getEmpName();
//            System.out.println("===stuName===>"+stuName);
            wrapper.like(!("".equals(empName)),"emp_name",empName);
            Page<Emp> page1 = empService.page(page, wrapper);
            List<Emp> records = page1.getRecords();
            return records.isEmpty()?Msg.fail():Msg.success()
                    .add("empList",records)
                    .add("sizePage",page.getSize())
                    .add("currentPage",page.getCurrent())
                    .add("pageTotal",page.getTotal());
        }
        return Msg.fail();
    }

    @PostMapping("/emp/addEmp")
    public Msg addEmp(@RequestBody Emp emp){
        boolean save = empService.save(emp);
        return save?Msg.success():Msg.fail();
    }

    @PostMapping("/emp/deleteEmp")
    public Msg deleteEmp(@RequestBody  Emp emp){
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.eq("emp_id",emp.getEmpId());
        boolean remove = empService.remove(wrapper);
        return remove?Msg.success():Msg.fail();
    }

    @PostMapping("/emp/updateEmp")
    public Msg updateEmp(@RequestBody Emp emp){
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.eq("emp_id",emp.getEmpId());
        return empService.update(emp,wrapper)?Msg.success():Msg.fail();
    }

    @PostMapping ("/emp/exportExcel")
    public Msg exportExcel(@RequestBody Excel excel){
        System.out.println(excel);
        List<Emp> list = empService.list();
        if(!("".equals(excel.getExcelFileName())) && !("".equals(excel.getExcelFilePath()))){
            String fileName = excel.getExcelFilePath() + excel.getExcelFileName()+ ".xlsx";
            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
            EasyExcel.write(fileName, Emp.class).sheet("员工表").doWrite(list);
            return Msg.success()
                    .add("fileSave",excel.getExcelFilePath() + excel.getExcelFileName()+ ".xlsx")
                    .add("fileSaveTips","小提示：若传入的是相对路径，请以本系统为根路径寻找");
        }
        return Msg.fail();
    }
    @PostMapping ("/emp/getEmpNameList")
    public Msg getEmpNameList(){

        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.select("emp_name","emp_id");
        List<Emp> list = empService.list(wrapper);
        return list.isEmpty()?Msg.fail():Msg.success().add("empNameList",list);
    }
}

